# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=fonttools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.22.1
pkgrel=1
pkgdesc="Converts OpenType and TrueType fonts to and from XML (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://github.com/fonttools/fonttools'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-brotli: WOFF 1.0 and WOFF 2.0 webfonts"
            "${MINGW_PACKAGE_PREFIX}-python-pyfilesystem2: for fontTools.ufoLib: to read/write UFO fonts"
            "${MINGW_PACKAGE_PREFIX}-python-pyzopfli: WOFF 1.0 and WOFF 2.0 webfonts"
            "${MINGW_PACKAGE_PREFIX}-python-sympy: for fontTools.misc.symfont, module for symbolic font statistics analysis")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-matplotlib"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest-runner")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.zip::https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.zip")
sha256sums=('fea6bb9d1d23a94d5e8fa6238214538a89d4333e340025a95a16b77ddb191e6f')

prepare() {
  rm -rf python-build-${CARCH}
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

#Enable tests when the dependencies are solved
check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py pytest || warning "Tests failed"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done

  install -D -m755 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
  chmod oga+r "${pkgdir}${MINGW_PREFIX}"/share/man/man1/ttx.1
}
